package main

import (
	"compress/handler"

	"compress/location"
	"fmt"
	"net/http"
	"os"
	"os/exec"
	"time"
)



func main() {


	http.Handle("/static/",
		http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))


	//http.HandleFunc("/imgs",handler.HTTPInterceptor(handler.GetLocationImgs))
	http.HandleFunc("/img",handler.HTTPInterceptor(handler.GetImgByPath))
	//http.HandleFunc("/config",handler.HTTPInterceptor(handler.GetConfig))
	//http.HandleFunc("/config/update",handler.HTTPInterceptor(handler.UpdateConfig))

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/static/view/", http.StatusMovedPermanently)
	})


	//http.HandleFunc("/comporess",handler.HTTPInterceptor(handler.CompressImgs))

	http.HandleFunc("/echo", handler.Echo)


	port := "520"
	//url := "http://127.0.0.1:"+port

	fmt.Println("需要浏览器打开")

	go start()





	err := http.ListenAndServe(":"+port,nil)
	if err != nil {
		fmt.Printf("启动失败,server error:%s \n",err)
	}




}

func start(){
	fmt.Println("Pid",os.Getpid())
	go location.UpdatePid(os.Getpid())

	time.Sleep(3*time.Second)
	fmt.Println("正在打开浏览器")
	cmd := exec.Command("open.bat")
	out, err := cmd.CombinedOutput()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(out))
}